package com.dc.designPattern._1_singleton.impl.type2;

/**
 * Description:  饿汉式(静态变量)
 * @author: gdc
 * @date: 2020/12/26
 * @version 1.0
 */
class Singleton {

	//1. 构造器私有化, 外部能new
	private Singleton() {}

	//2.本类内部创建对象实例
	private  static Singleton INSTANCE;

	// 在静态代码块中，创建单例对象
	static {
		INSTANCE = new Singleton();
	}

	//3. 提供一个公有的静态方法，返回实例对象
	public static Singleton getINSTANCE() {
		return INSTANCE;
	}

}

public class SingletonTest02 {

	public static void main(String[] args) {
		//测试
		Singleton instance = Singleton.getINSTANCE();
		Singleton instance2 = Singleton.getINSTANCE();
		System.out.println(instance == instance2); // true
		System.out.println("INSTANCE.hashCode=" + instance.hashCode());
		System.out.println("instance2.hashCode=" + instance2.hashCode());
	}

}

